package com.haier.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haier.constant.HpConstant;
import com.haier.dto.HpGoApplyDto;
import com.haier.entity.HpArea;
import com.haier.entity.HpDayCount;
import com.haier.entity.HpGoApply;
import com.haier.entity.HpHoursCount;
import com.haier.entity.HpMonthCount;
import com.haier.mapper.HpAreaGoTrackHistoryMapper;
import com.haier.mapper.HpAreaGoTrackMapper;
import com.haier.mapper.HpAreaMapper;
import com.haier.mapper.HpDayCountMapper;
import com.haier.mapper.HpGoApplyMapper;
import com.haier.mapper.HpHoursCountMapper;
import com.haier.mapper.HpMonthCountMapper;
import com.haier.service.IHpGoApplyService;
import com.haier.service.ITaskService;
import com.haier.utils.EmptyUtils;
import com.haier.utils.GetUtils;

/**
 * <p>
 * 区域 服务实现类
 * </p>
 *
 * @author luanxuecheng
 * @since 2020-07-01
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class TaskServiceImpl implements ITaskService {
	@Autowired
	private HpAreaMapper hpAreaMapper;
	@Autowired
	private HpAreaGoTrackMapper hpAreaGoTrackMapper;
//	@Autowired
//	private HpAreaGoTrackHistoryMapper hpAreaGoTrackHistoryMapper;
	@Autowired
	private HpHoursCountMapper hpHoursCountMapper;
	@Autowired
	private HpDayCountMapper hpDayCountMapper;
	@Autowired
	private HpMonthCountMapper hpMonthCountMapper;
	@Autowired
	private HpGoApplyMapper hpGoApplyMapper;
	@Autowired
	private IHpGoApplyService iHpGoApplyService;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void countByHours() {
		Long thisTime = System.currentTimeMillis();
		Integer hoursTap = 3600000;
		Date yesterday = new Date(thisTime - 86400000);// 昨天
		String yesterdayStr = GetUtils.getDateString(yesterday, "yyyy-MM-dd");
		Date startDate = GetUtils.getDate(yesterdayStr, "yyyy-MM-dd");// 获取昨天的开始时间
		Long startTime = startDate.getTime();// 获取开始时间

		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("is_del", false);
		List<HpArea> areaList = hpAreaMapper.selectList(queryWrapper);
		if (!EmptyUtils.isEmpty(areaList)) {
			queryWrapper = new QueryWrapper();
			for (int i = 0; i < 24; i++) {// 循环24次插入时间hours
				Date date = new Date(startTime + i * hoursTap);
				String hours = GetUtils.getDateString(date, "yyyy-MM-dd HH");
				for (HpArea area : areaList) {
					queryWrapper = new QueryWrapper();
					queryWrapper.eq("is_success", true);
					queryWrapper.eq("DATE_FORMAT(create_time, '%Y-%m-%d %H')", hours);
					queryWrapper.eq("area_id", area.getId());
					Integer count = hpAreaGoTrackMapper.selectCount(queryWrapper);
					queryWrapper = new QueryWrapper();
					queryWrapper.eq("hp_area_id", area.getId());
					queryWrapper.eq("hp_hours", hours);
					HpHoursCount hhc = hpHoursCountMapper.selectOne(queryWrapper);
					if (hhc == null) {
						hhc = new HpHoursCount();
						hhc.setHpAreaId(area.getId());
						hhc.setHpCount(count);
						hhc.setHpHours(hours);
						hpHoursCountMapper.insert(hhc);
					} else {
						hhc.setHpCount(count);
						hpHoursCountMapper.updateById(hhc);
					}
				}
			}
			queryWrapper = new QueryWrapper();
			String day = GetUtils.getDateString(yesterday, "yyyy-MM-dd");
			for (HpArea area : areaList) {
				queryWrapper = new QueryWrapper();
				queryWrapper.eq("is_success", true);
				queryWrapper.eq("DATE_FORMAT(create_time, '%Y-%m-%d')", day);
				queryWrapper.eq("area_id", area.getId());
				Integer count = hpAreaGoTrackMapper.selectCount(queryWrapper);

				queryWrapper = new QueryWrapper();
				queryWrapper.eq("hp_area_id", area.getId());
				queryWrapper.eq("hp_day", day);
				HpDayCount hdc = hpDayCountMapper.selectOne(queryWrapper);
				if (hdc == null) {
					hdc = new HpDayCount();
					hdc.setHpAreaId(area.getId());
					hdc.setHpCount(count);
					hdc.setHpDay(day);
					hpDayCountMapper.insert(hdc);
				} else {
					hdc.setHpCount(count);
					hpDayCountMapper.updateById(hdc);
				}
			}
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void countByMonth() {
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, -1);
		Date lastMonth = c.getTime();
		String month = GetUtils.getDateString(lastMonth, "yyyy-MM");

		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("is_del", false);
		List<HpArea> areaList = hpAreaMapper.selectList(queryWrapper);// 查询全部的区域列表
		if (!EmptyUtils.isEmpty(areaList)) {
			queryWrapper = new QueryWrapper();
			for (HpArea area : areaList) {
				queryWrapper = new QueryWrapper();
				queryWrapper.eq("is_success", true);
				queryWrapper.eq("DATE_FORMAT(create_time, '%Y-%m')", month);
				queryWrapper.eq("area_id", area.getId());
				Integer count = hpAreaGoTrackMapper.selectCount(queryWrapper);// 查询当月的数量

				queryWrapper = new QueryWrapper();
				queryWrapper.eq("hp_area_id", area.getId());
				queryWrapper.eq("hp_month", month);
				HpMonthCount hmc = hpMonthCountMapper.selectOne(queryWrapper);// 查询是否已添加 已添加则覆盖
				if (hmc == null) {
					hmc = new HpMonthCount();
					hmc.setHpAreaId(area.getId());
					hmc.setHpCount(count);
					hmc.setHpMonth(month);
					hpMonthCountMapper.insert(hmc);
				} else {
					hmc.setHpCount(count);
					hpMonthCountMapper.updateById(hmc);
				}
			}
		}
		// 挪动数据
//		hpAreaGoTrackHistoryMapper.insertByMonth(month);
//		hpAreaGoTrackMapper.deleteByMonth(month);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void reject() {
		try {
			Date date = new Date();
			String day = GetUtils.getDateString(date, "yyyy-MM-dd");
			QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.eq("status", HpConstant.HP_GO_APPLY_STATUS_10);
			queryWrapper.lt("DATE_FORMAT(create_time, '%Y-%m-%d')", day);
			queryWrapper.groupBy("auditor");
			List<HpGoApply> applyList = hpGoApplyMapper.selectList(queryWrapper);
			if (!EmptyUtils.isEmpty(applyList)) {
				for (HpGoApply apply : applyList) {
					List<Long> idList = hpGoApplyMapper.findOldByAuditor(apply.getAuditor(), day,
							HpConstant.HP_GO_APPLY_STATUS_10);
					Long[] ids = idList.toArray(new Long[idList.size()]);
					HpGoApplyDto dto = new HpGoApplyDto();
					dto.setIds(ids);
					dto.setStatus(HpConstant.HP_GO_APPLY_STATUS_F10);
					dto.setAuditor(apply.getAuditor());
					iHpGoApplyService.setStatus(dto);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
